package cn.zzszxyy.hr.controller;

import cn.zzszxyy.hr.comm.Global;
import cn.zzszxyy.hr.model.MenuEntity;
import cn.zzszxyy.hr.model.RoleEntity;
import cn.zzszxyy.hr.model.UserEntity;
import cn.zzszxyy.hr.service.MenuService;
import cn.zzszxyy.hr.service.RoleService;
import cn.zzszxyy.hr.support.AuthPassport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ExtendedModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 作者:张伟
 * <p>
 * 联系:adamzzww@163.com
 * 时间:2016-11-29
 * 说明:角色
 */
@Controller
@RequestMapping(Global.PATH)
public class RoleController extends ControllerEx {

    private final String path = "/role";

    @Autowired
    private RoleService roleService;

    @Autowired
    private MenuService menuService;

    @AuthPassport
    @RequestMapping(value = path + "/list", method = RequestMethod.GET)
    public String get(HttpServletRequest request, ExtendedModelMap map) {
        Integer pageIndex = getPageIndex(request);

        map.put("page", roleService.findAllWithPage(pageIndex, Global.PAGE_SIZE));
        map.put("pageIndex", pageIndex);
        return Global.DESKTOP + path + "/role-list";
    }

    @RequestMapping(value = path + "/{id}", method = RequestMethod.GET)
    public String get(@PathVariable int id, ExtendedModelMap map) {
        RoleEntity entity = roleService.findOne(id);
        map.put("entity", entity);
        // 菜单
        List<MenuEntity> menuList = menuService.findMenusByRoleId(id);
        String menusName = "";
        String menusId = "";
        for (MenuEntity menu : menuList) {
            menusName = menusName + menu.getMenuName() + ";";
            menusId = menusId + menu.getMenuId().toString() + ";";
        }
        map.put("menusName", menusName);
        map.put("menusId", menusId);

        return Global.DESKTOP + path + "/role-edit";
    }

    @RequestMapping(value = path + "/add", method = RequestMethod.GET)
    public String add(ExtendedModelMap map) {
        List<MenuEntity> menuList = menuService.findAll();
        map.put("menuList", menuList);

        return Global.DESKTOP + path + "/role-edit";
    }

    @ResponseBody
    @RequestMapping(value = path, method = {RequestMethod.PUT, RequestMethod.POST})
    public String post(HttpServletRequest request, RoleEntity roleEntity, String menusId) {
        UserEntity user = (UserEntity) request.getSession().getAttribute(Global.COOKIE_USER_OBJECT);
        if (menusId.length() > 0) {
            menusId = menusId.substring(0, menusId.length() - 1);
        }
        roleEntity.setCreateTime(new Date());
        roleEntity.setCreator(user.getUserName());
        if (roleService.save(roleEntity, menusId) != null) {
            return Global.RE_SUCCESS;
        } else {
            return Global.RE_ERROR;
        }
    }

    @ResponseBody
    @RequestMapping(value = path + "/{id}", method = RequestMethod.DELETE)
    public String delete(@PathVariable int id) {
        roleService.delete(id);
        return Global.RE_SUCCESS;//"redirect:" + Global.Web.PATH + path;
    }
}
